<template>
  <div class="home">
    <!-- 导航栏 -->
    <NavBar />
    
    <!-- 主要内容 -->
    <div class="main-content">
      <!-- 英雄区域 -->
      <section class="hero">
        <div class="hero-content">
          <h1 class="hero-title">现代化博客系统</h1>
          <p class="hero-subtitle">分享你的想法，记录你的生活</p>
          <div class="hero-actions">
            <el-button type="primary" size="large" @click="$router.push('/posts')">
              浏览文章
            </el-button>
            <el-button size="large" @click="$router.push('/write')" v-if="userStore.isLoggedIn">
              写文章
            </el-button>
            <el-button size="large" @click="$router.push('/login')" v-else>
              开始使用
            </el-button>
          </div>
        </div>
      </section>

      <!-- 特色文章 -->
      <section class="featured-posts" v-if="featuredPosts.length > 0">
        <div class="container">
          <h2 class="section-title">精选文章</h2>
          <div class="posts-grid">
            <PostCard 
              v-for="post in featuredPosts" 
              :key="post.id" 
              :post="post"
              @click="$router.push(`/posts/${post.id}`)"
            />
          </div>
        </div>
      </section>

      <!-- 最新文章 -->
      <section class="latest-posts">
        <div class="container">
          <h2 class="section-title">最新文章</h2>
          <div class="posts-list">
            <PostCard 
              v-for="post in latestPosts" 
              :key="post.id" 
              :post="post"
              @click="$router.push(`/posts/${post.id}`)"
            />
          </div>
          <div class="view-more">
            <el-button @click="$router.push('/posts')">查看更多</el-button>
          </div>
        </div>
      </section>
    </div>

    <!-- 页脚 -->
    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { usePostsStore } from '@/stores/posts'
import { useUserStore } from '@/stores/user'
import NavBar from '@/components/NavBar.vue'
import Footer from '@/components/Footer.vue'
import PostCard from '@/components/PostCard.vue'
import type { Post } from '@/types'

const postsStore = usePostsStore()
const userStore = useUserStore()

const featuredPosts = ref<Post[]>([])
const latestPosts = ref<Post[]>([])

onMounted(async () => {
  try {
    // 获取精选文章
    await postsStore.fetchPosts({ limit: 3, published_only: true })
    featuredPosts.value = postsStore.posts.filter(post => post.is_featured)
    
    // 获取最新文章
    await postsStore.fetchPosts({ limit: 6, published_only: true })
    latestPosts.value = postsStore.posts
  } catch (error) {
    console.error('获取文章失败:', error)
  }
})
</script>

<style scoped>
.home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
}

.hero {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 120px 0;
  text-align: center;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
}

.hero-title {
  font-size: 3.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  line-height: 1.2;
}

.hero-subtitle {
  font-size: 1.25rem;
  margin-bottom: 2rem;
  opacity: 0.9;
}

.hero-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
}

.featured-posts,
.latest-posts {
  padding: 80px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-title {
  font-size: 2.5rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 3rem;
  color: #333;
}

.posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 2rem;
  margin-bottom: 3rem;
}

.posts-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
  margin-bottom: 3rem;
}

.view-more {
  text-align: center;
}

@media (max-width: 768px) {
  .hero-title {
    font-size: 2.5rem;
  }
  
  .hero-actions {
    flex-direction: column;
    align-items: center;
  }
  
  .posts-grid,
  .posts-list {
    grid-template-columns: 1fr;
  }
}
</style>
